package com.javacoo.xservice.base.support.lock.internal;

import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import lombok.extern.slf4j.Slf4j;

/**
 * Redission配置类
 * <li></li>
 *
 * @author: duanyong@jccfc.com
 * @since: 2020/6/22 10:41
 */
@Slf4j
@Configuration
public class RedissionConfig {
    @Autowired
    private RedisProperties redisProperties;
    /**
     * 单机模式 redisson 客户端
     * <li></li>
     * @author duanyong@jccfc.com
     * @date 2020/6/22 11:32
     * @return: org.redisson.api.RedissonClient
     */
    @Bean
    public RedissonClient redissonSingle() {
        Config config = new Config();
        String node = redisProperties.getHost().startsWith("redis://") ? redisProperties.getHost() : "redis://" + redisProperties.getHost()+":"+ redisProperties.getPort();
        //String node = redisProperties.getHost()+":"+ redisProperties.getPort();
        SingleServerConfig serverConfig = config.useSingleServer()
            .setAddress(node);
            //.setTimeout(redisConfig.getTime out())
            //.setConnectionPoolSize(redisConfig.getPool().getMaxActive())
            //.setConnectionMinimumIdleSize(redisConfig.getPool().getMinIdle());
        if (StringUtils.isNotBlank(redisProperties.getPassword())) {
            serverConfig.setPassword(redisProperties.getPassword());
        }
        try {
            return Redisson.create(config);
        } catch (Exception e) {
            log.error("RedissonClient init redis url:[{}], Exception:", node, e);
            return null;
        }
    }

}
